const app = getApp();
var api = require('../../api.js');

Page({
  data: {
    list:[],
    str:{
      lesson_id: '1',
      id: '1',
    },
  },
  onLoad: function (options) {

    app.bind();
    this.getHistory();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
    
    app.bind();
    this.getHistory();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };
  },
  getHistory: function (e) {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.study.history,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        console.log(e.data.data);
        if (e.data.code == 0) {
          that.setData({
            list: e.data.data
          })
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
  continue: function (e) {
    var that = this;
    var cate = e.currentTarget.dataset.cate;
    var lesson = e.currentTarget.dataset.lesson;
    var section = e.currentTarget.dataset.section;
    var str = that.data.str;
    str.lesson_id = lesson;
    str.id = section;
    str = JSON.stringify(str);
    // console.log(str);
    if (cate == 1) {
      // console.log("免费专栏");
      wx.navigateTo({
        url: '/pages/freeDetail/freeDetail?str=' + str,
      })
    } else if (cate == 2) {
      // console.log("课程专栏");
      wx.navigateTo({
        url: '/pages/classDetail/classDetail?str=' + str,
      })
    } else if (cate == 3) {
      // console.log("视频专栏");
      wx.navigateTo({
        url: '/pages/moviesDetail/moviesDetail?str=' + str,
      })
    }
  },
})